package practice.unionfind;

/**
 * @Description:
 * @Author Ammar
 * @Create 2023/8/26 19:29
 */
public class QuickFind extends AbstractUnionFind {
    public QuickFind(int capacity) {
        super(capacity);
    }

    @Override
    public int find(int v) {
        rangeCheck(v);
        return parent[v];
    }

    @Override
    public void union(int v1, int v2) {
        int p1 = find(v1);
        int p2 = find(v2);
        if (p1 == p2) return;
        for (int i = 0; i < parent.length; i++) {
            if (parent[i] == p1) {
                parent[i] = p2;
            }
        }
    }
}
